package com.homegoods.discover.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.homegoods.area.model.SubModelList;
import com.homegoods.classify.model.Constant;
import com.homegoods.discover.DiscoverApiUtils;
import com.homegoods.discover.item.ADViewItem;
import com.homegoods.discover.model.BannerModel;
import com.homegoods.discover.model.BestCollectModel;
import com.homegoods.discover.model.VideoModel;
import com.homegoods.resource.ILifecycle;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: BestCollectionPresenter
 * @date 2018/9/10
 * @description 精选Presenter层
 */
public class BestCollectionPresenter extends AbstractRefreshPresenter<RefreshingView> implements ILifecycle {
    private ADViewItem adViewItem;
    private Random random = new Random();

    public BestCollectionPresenter(RefreshingView mView) {
        super(mView);
        adViewItem = new ADViewItem(null);
    }

    @Override
    protected void loadData(boolean loadMore) {
        addSubscription(Observable.timer(1000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    if (loadMore) {
                        loading = false;
                    } else {
                        onStop();
                        refreshing = false;
                        adapter.setItem(adViewItem);
                        adapter.notifyItemInserted(0);
//                        获取头部数据
                        getHeadViewData();
                    }
                    fetchData(loadMore);
                }));
    }

    @Override
    protected void fetchData(final boolean loadMore) {
        //todo 此部分只请求热门分类数据
//        addSubscription(loadBestCollect().subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(bestCollectModels -> parserData(loadMore, bestCollectModels)));
        DiscoverApiUtils.getDiscoverNewsList(bestCollectModelSubModelList -> {
            List<BestCollectModel> list = new ArrayList<>(JSONArray.parseArray(bestCollectModelSubModelList.getItems(), BestCollectModel.class));
            parserData(loadMore, list);
        }, throwable -> {

        });
    }

//    private void getDiscoverNewsListSuc(SubModelList<BestCollectModel> subModelList) {
//        Log.e("--", "-*-*-*-" + JSON.toJSON(subModelList));
//    }

    /**
     * 在此请求轮播图数据
     */
    private void getHeadViewData() {
        //todo 每次刷新的时候，
//        addSubscription(Observable.range(0, 4)
//                .map(i -> {
//                    BannerModel data = new BannerModel();
//                    data.setImgUr(Constant.TEST_AD_URL + random.nextInt(1000));
//                    data.setTitle(String.format("标题：%s", random.nextInt(100)));
//                    return data;
//                })
//                .toList()
//                .subscribe(strings -> {
//                    adViewItem.setImages(strings);
//                    adViewItem.start();
//                }));
        DiscoverApiUtils.getDiscoverNewsAdPage(this::getDiscoverNewsAdPageSuc, throwable -> {

        });

    }

    private void getDiscoverNewsAdPageSuc(SubModelList subModelList) {
//        Log.e("--", "-*-*-*-" + JSON.toJSON(subModelList));
//        List<BestCollectModel> list = JSON.parseArray(JSON.toJSON(subModelList.getItems()),);
        List<BestCollectModel> list = JSON.parseArray(subModelList.getItems(), BestCollectModel.class);
        addSubscription(Observable.range(0, list.size())
                .map(i -> {
                    BannerModel data = new BannerModel();
                    data.setImgUr(Constant.SERVER_URL + list.get(i).getPublicimg());
                    data.setTitle(list.get(i).getTitle());
                    data.setId(list.get(i).getSid());
                    return data;
                })
                .toList()
                .subscribe(strings -> {
                    adViewItem.setImages(strings);
                    adViewItem.start();
                }));
    }

//    private Observable<List<BestCollectModel>> loadBestCollect() {
//        return Observable.range(0, 12)
//                .map(integer -> {
//                    BestCollectModel model = new BestCollectModel();
//                    model.setTitle("热门产品" + integer);
//                    model.setImgUrl(Constant.TEST_IMG_URL + random.nextInt(1000));
//                    model.setMsgCount(String.valueOf(random.nextInt(1000)));
//                    model.setOrigin("宅品直营");
//                    return model;
//                })
//                .buffer(12);
//    }

    @Override
    public void onStart() {
        adViewItem.start();
    }

    @Override
    public void onStop() {
        adViewItem.stop();
    }

    @Override
    public void onDestroy() {

    }
}
